{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd5a518b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://huggingface.co/datasets/MahiA/VocalSound/resolve/main/VocalSound.zip\n",
    "# !unzip VocalSound.zip\n",
    "!rm VocalSound.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6c28baa1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 1.6M\r\n",
      "drwxrwxrwx   4 mesolitica mesolitica 4.0K Nov  1  2024 .\r\n",
      "drwxrwxr-x 131 mesolitica mesolitica  20K Jun  8 11:16 ..\r\n",
      "drwxrwxrwx   2 mesolitica mesolitica 872K Sep 28  2024 audios\r\n",
      "drwxrwxrwx   3 mesolitica mesolitica 4.0K Nov  1  2024 .cache\r\n",
      "-rw-rw-rw-   1 mesolitica mesolitica 127K Sep 29  2024 test.csv\r\n",
      "-rw-rw-rw-   1 mesolitica mesolitica 549K Sep 29  2024 train.csv\r\n"
     ]
    }
   ],
   "source": [
    "!ls -lha VocalSound"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "01bdf300",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>path</th>\n",
       "      <th>classname</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>audios/m0379_0_laughter.wav</td>\n",
       "      <td>Laughter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>audios/m0758_0_sigh.wav</td>\n",
       "      <td>Sigh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>audios/m0446_0_sniff.wav</td>\n",
       "      <td>Sniff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>audios/f0416_0_throatclearing.wav</td>\n",
       "      <td>Throat clearing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>audios/f1881_0_sneeze.wav</td>\n",
       "      <td>Sneeze</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15526</th>\n",
       "      <td>audios/m2485_0_sniff.wav</td>\n",
       "      <td>Sniff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15527</th>\n",
       "      <td>audios/f0919_0_sniff.wav</td>\n",
       "      <td>Sniff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15528</th>\n",
       "      <td>audios/m2247_0_sneeze.wav</td>\n",
       "      <td>Sneeze</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15529</th>\n",
       "      <td>audios/f0146_0_sigh.wav</td>\n",
       "      <td>Sigh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15530</th>\n",
       "      <td>audios/f0793_0_cough.wav</td>\n",
       "      <td>Cough</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15531 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    path        classname\n",
       "0            audios/m0379_0_laughter.wav         Laughter\n",
       "1                audios/m0758_0_sigh.wav             Sigh\n",
       "2               audios/m0446_0_sniff.wav            Sniff\n",
       "3      audios/f0416_0_throatclearing.wav  Throat clearing\n",
       "4              audios/f1881_0_sneeze.wav           Sneeze\n",
       "...                                  ...              ...\n",
       "15526           audios/m2485_0_sniff.wav            Sniff\n",
       "15527           audios/f0919_0_sniff.wav            Sniff\n",
       "15528          audios/m2247_0_sneeze.wav           Sneeze\n",
       "15529            audios/f0146_0_sigh.wav             Sigh\n",
       "15530           audios/f0793_0_cough.wav            Cough\n",
       "\n",
       "[15531 rows x 2 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('VocalSound/train.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cda854cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████| 15531/15531 [00:00<00:00, 21863.93it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "import random\n",
    "import json\n",
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "questions = [\n",
    "    'given the labels\\n{labels}\\n\\nwhat is the label for the audio',\n",
    "    'what is the label for audio\\n\\nthe labels: {labels}'\n",
    "]\n",
    "\n",
    "df = pd.read_csv('VocalSound/train.csv')\n",
    "unique_labels = str(df['classname'].unique().tolist())\n",
    "\n",
    "train = []\n",
    "for i in tqdm(range(len(df))):\n",
    "    metadata = df.iloc[i].to_dict()\n",
    "    audio_filename = os.path.join('VocalSound', df['path'].iloc[i])\n",
    "    if not os.path.exists(audio_filename):\n",
    "        continue\n",
    "    \n",
    "    train.append({\n",
    "        'question': random.choice(questions).format(labels = unique_labels),\n",
    "        'answer': df['classname'].iloc[i],\n",
    "        'audio_filename': audio_filename,\n",
    "        'metadata': json.dumps(metadata)\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "45a0dc71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'question': \"what is the label for audio\\n\\nthe labels: ['Laughter', 'Sigh', 'Sniff', 'Throat clearing', 'Sneeze', 'Cough']\",\n",
       " 'answer': 'Laughter',\n",
       " 'audio_filename': 'VocalSound/audios/m0379_0_laughter.wav',\n",
       " 'metadata': '{\"path\": \"audios/m0379_0_laughter.wav\", \"classname\": \"Laughter\"}'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0f680e02",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████| 3591/3591 [00:00<00:00, 21633.57it/s]\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('VocalSound/test.csv')\n",
    "\n",
    "test = []\n",
    "for i in tqdm(range(len(df))):\n",
    "    metadata = df.iloc[i].to_dict()\n",
    "    audio_filename = os.path.join('VocalSound', df['path'].iloc[i])\n",
    "    if not os.path.exists(audio_filename):\n",
    "        continue\n",
    "    \n",
    "    test.append({\n",
    "        'question': random.choice(questions).format(labels = unique_labels),\n",
    "        'answer': df['classname'].iloc[i],\n",
    "        'audio_filename': audio_filename,\n",
    "        'metadata': json.dumps(metadata)\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "56b35e52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3591"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6f8c2b10",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import Dataset\n",
    "\n",
    "dataset = Dataset.from_list(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2875133f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d6c011325a944ce2b5a9864d9e09e41c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e6af8513a6c7451182feef9f95084dcf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/16 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "299bb7a06c694a749c4f7ad2e58cbbee",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading...:   0%|          | 0.00/402k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "823c63a6d9bf4a28a3f6d585aed2271e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/1.76k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/commit/01ea3624fd5bd1ebb37c5fdcef5d2492ee60dba5', commit_message='Upload dataset', commit_description='', oid='01ea3624fd5bd1ebb37c5fdcef5d2492ee60dba5', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions', endpoint='https://huggingface.co', repo_type='dataset', repo_id='mesolitica/Zeroshot-Audio-Classification-Instructions'), pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.push_to_hub('mesolitica/Zeroshot-Audio-Classification-Instructions', split = 'vocalsound_train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "14dc531d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0335727690204da592675cfd64b4fc2a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "975e61a7c99f459c9c84c82449fadaea",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/4 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e5274c5f5b344c1fa1e2e176abfff0a5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading...:   0%|          | 0.00/89.3k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7a23fe0e6e87414d8c34cd57a65171c5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/1.90k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/commit/4ec02c5db168021ec69effcad2a48ff1e2cf4260', commit_message='Upload dataset', commit_description='', oid='4ec02c5db168021ec69effcad2a48ff1e2cf4260', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions', endpoint='https://huggingface.co', repo_type='dataset', repo_id='mesolitica/Zeroshot-Audio-Classification-Instructions'), pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset = Dataset.from_list(test)\n",
    "dataset.push_to_hub('mesolitica/Zeroshot-Audio-Classification-Instructions', split = 'vocalsound_test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "12987e2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21024"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glob import glob\n",
    "\n",
    "audio_files = glob('VocalSound/audios/*.wav')\n",
    "len(audio_files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3a9d52be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import zipfile\n",
    "with zipfile.ZipFile('vocalsound.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
    "    for f in audio_files:\n",
    "        zipf.write(f, arcname=f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5b4ffdf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading files using Xet Storage..\n",
      "Uploading...: 100%|████████████████████████| 4.78G/4.78G [01:09<00:00, 68.7MB/s]\n",
      "https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/blob/main/vocalsound.zip\n"
     ]
    }
   ],
   "source": [
    "!huggingface-cli upload mesolitica/Zeroshot-Audio-Classification-Instructions vocalsound.zip \\\n",
    "--repo-type=dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5eb6dd48",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm vocalsound.zip"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
